from literature.models.GlobalTables import *
from dataset.models import *
from control.models import *
import json

def init_from_json(file_name, model, user):
    with open(file_name, 'r', encoding="utf-8") as f:
        data = json.load(f)
    for item in data:
        model.objects.get_or_create(**item, creator=user, last_editor=user)

def init_literature_global_table(user):
    init_from_json("files/confer_meeting_cycle_data.json", ConferenceMeetingCycleType, user)
    init_from_json("files/department_type_data.json", DepartmentType, user)
    init_from_json("files/institution_type_data.json", InstitutionType, user)
    init_from_json("files/journal_pub_cycle_data.json", JournalPublicationCycleType, user)
    init_from_json("files/keyword_type_data.json", KeywordType, user)
    init_from_json("files/language_data.json", Language, user)
    init_from_json("files/liter_status_data.json", LiteratureStatusType, user)
    init_from_json("files/liter_type_data.json", LiteratureType, user)

def init_dataset_global_table(user):
    init_from_json("files/data_definition_way_data.json", DataDefinitionWay, user)
    init_from_json("files/data_present_way_data.json", DataPresentWay, user)
    init_from_json("files/exp_purpose_data.json", ExpPurpose, user)
    init_from_json("files/sample_source_data.json", SampleSource, user)

def init_global_table_content():
    super_admin_entity = User.objects.filter(user_account="superadmin").first()
    init_literature_global_table(super_admin_entity)
    init_dataset_global_table(super_admin_entity)
    
    print("Global table initialization completed!")